<?php


namespace app\admin\job;
use app\service\propertyService;
use app\service\RedisQueueService;
use app\zftpay\service\apiService;
use think\facade\Db;
use think\queue\Job;

//账单结算
class BillpaySettle
{

    /**
     * fire方法是消息队列默认调用的方法
     * @param Job            $job      当前的任务对象
     * @param array|mixed    $data     发布任务时自定义的数据
     */
    public function fire(Job $job,$data){
        // 如有必要,可以根据业务需求和数据库中的最新数据,判断该任务是否仍有必要执行.
        //执行收单交易结算
        //php  think queue:work --queue BillpaySettleQueue

        $isJobDone = $this->doJob($data);
        print("<info>BillpaySettle Job Started. job Data is: ".var_export($isJobDone,true)."</info> \n");

        if ($isJobDone) {
            $job->delete();
        }else{
            if ($job->attempts() > 3) {
                //记录开票推送失败的记录
                $job->delete();

                (new RedisQueueService())->push("BillPaySettleFailed",$data["id"]);

            }
        }
    }

    public function doJob($data){
        $billpay=Db::table("billpay")->where("id",$data["id"])->find();
        if(empty($billpay)){
            return (new RedisQueueService())->push("BillPaySettleFailed",$data["id"]);
        }
        if($billpay["settle_status"]==1){
            return true;
        }
        //查询该账单是否已经结算
        $res=Db::table("zft_settle_order")->where("billpay_id",$billpay["id"])->find();
        if(!empty($res)){
            return true;
        }
        $propertyZftInfo=(new propertyService())->propertyZftByCommunityId($billpay["community_id"]);
        if(empty($propertyZftInfo)){
            print("<info>Hello Job Started. job Data is: 直付通配置为空</info> \n");
            return json(['info' => '直付通配置为空']);
        }
        $api=new apiService();
        $amount=round($propertyZftInfo["taxrate"]*$billpay["pay_money"],2);
        if($amount<=0){
            Db::table("billpay")->where("id",$data["id"])->update([
                "settle_status"=>1
            ]);
            return true;
        }
        $royalty_parameters=array(
            'royalty_type'=>'transfer',//'loginName',
            'trans_in_type'=>'loginName',//收入方账户类型
            'trans_in'=>'aliwykd@163.com',//收入方账户\
            'amount'=>$amount,//分账金额
        );
        $data= array(
            'out_trade_no'=> $billpay["out_trade_no"],
            'trade_no'=>$billpay["trade_no"],
            'royalty_parameters'=>$royalty_parameters,
        );

        $result = $api->settle(json_encode($data));
        if($result->code==10000){
            $settle["community_id"]=$billpay["community_id"];
            $settle["trade_no"]=$billpay["trade_no"];
            $settle["billpay_id"]=$billpay["id"];
            $settle["order_money"]=$billpay["pay_money"];
            $settle["out_room_id"]=$billpay["out_room_id"];
            $settle["settle_money"]=$amount;
            $settle["out_room_id"]=$billpay["out_room_id"];
            $settle["settle_no"]=$result->settle_no;
            $settle["createTime"]=time();
            $res=Db::table("zft_settle_order")->insert($settle);
            if(!empty($res)){
                $query["settle_no"]=$settle["settle_no"];
                $api=new apiService();
                $queryresult = $api->settleQuery(json_encode($query));
                if($queryresult->code==10000){
                    Db::table("zft_settle_order")->where("billpay_id",$billpay["id"])->update(["status"=>1]);
                    Db::table("billpay")->where("id",$billpay["id"])->update(["settle_status"=>1]);
                }
            }
        }
        print("<info>Hello Job Started. job Data is: ".var_export($result,true)."</info> \n");


        return true;
    }
}